//
//  PlayerViewBottomView.swift
//  HaiLuo
//
//  Created by Rowling on 2020/5/29.
//  Copyright © 2020 Rowling. All rights reserved.
//

import UIKit


protocol PlayerViewBottomViewDelegate : NSObjectProtocol {
    func onClickedfullScreenButton()
    func sliderDidEndDrag(value : Float)
    func sliderVauleDidChange(value : Float)
    func playBtnClick(button : UIButton)
    func listButtonClick()
    func shareButtonClick()
    func lickButtonClick(button : UIButton)
    func downLoadClick(button : UIButton)
    func muteClick(button : UIButton)
    /**播放下集*/
    func playNextClick()
}

class PlayerViewBottomView: UIView {

    enum BottomType {
        /**剧集*/
        case drama
        /**普通*/
        case normal
        /**下载*/
        case download
    }
    
    let disposeBag = DisposeBag()
    
    var ProgressView : AliyunPlayerViewProgressView!
    
    var bgMaskView : UIImageView!
    var stack: UIStackView!
    
    var playBtn : UIButton!
    var playTimeLabel : UILabel!
    var allTimeLabel : UILabel!
    var xuanzhuanBtnn : UIButton!
    
    var downButton : UIButton! //下载按钮
    var likeButton : UIButton! // 收藏按钮
    var shareButton : UIButton! // 分享按钮
    var listButton : UIButton! // 剧集按钮
    var nextButton : UIButton! //下一集
    var muteButton : UIButton! //静音

    var isProtrait = true {
        didSet {
            if isProtrait {
//                nextButton.isHidden = true
//                xuanzhuanBtnn.isHidden = true
//                downButton.isHidden = true
//                likeButton.isHidden = true
//                shareButton.isHidden = true
//                listButton.isHidden = true
            } else {
//                landScapeUI()
            }
        }
    } // 是否是竖屏
    var isDrag = false  // 是否处于拖动中呢
    
    weak var delegate : PlayerViewBottomViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        // maskLayer
        bgMaskView = UIImageView.init(image: UIImage.init(named: "mengc"))

        stack = UIStackView()
        stack.spacing = 0.0
        stack.alignment = .center
        stack.axis = .horizontal
        
        // 播放，暂停
        playBtn = ZMUtil.button(image: UIImage.init(named: "zt"))
        playBtn.setImage(UIImage.init(named: "bofang"), for: .selected)
        
        //下一集
        nextButton = ZMUtil.button(image: UIImage.init(named: "kuaijin"))
        nextButton.isHidden = true
        
        //播放时间
        playTimeLabel = ZMUtil.label(title: "00:00", color: "#FFFDFC".zm.hex(), font: UIFont.systemFont(ofSize: 12, weight: .medium))
        playTimeLabel.zm.setWidth(40)
        
        /// 进度条
        ProgressView = AliyunPlayerViewProgressView()
        ProgressView.playSlider.minimumTrackTintColor = "#3BBB0D".zm.hex()
        ProgressView.delegate = self
        ProgressView.loadTimeProgress = 0

        //总时间
        allTimeLabel = ZMUtil.label(title: "00:00", color: "#FFFDFC".zm.hex(), font: UIFont.systemFont(ofSize: 12, weight: .medium))
        allTimeLabel.textAlignment = .center
        allTimeLabel.zm.setWidth(40)
        
        //静音
        muteButton = ZMUtil.button(image: UIImage.init(named: "icon_close_volume_1"))
        muteButton.setImage(UIImage.init(named: "icon_close_volume"), for: .selected)
        muteButton.isSelected = true
        muteButton.isHidden = false
        
        //旋转
        xuanzhuanBtnn = ZMUtil.button(image: UIImage.init(named: "xuanzhuan"))
        xuanzhuanBtnn.isHidden = true
        
        //下载按钮
        downButton = ZMUtil.button(image: UIImage.init(named: "xiazai_baise"))
        downButton.isHidden = true
        
        //收藏按钮
        likeButton = ZMUtil.button(image: UIImage.init(named: "sc_bs"))
        likeButton.setImage(UIImage.init(named: "redian_yidianzan"), for: .selected)
        likeButton.isHidden = true
        
        // 分享按钮
        shareButton = ZMUtil.button(image: UIImage.init(named: "fenxiang_bs"))
        shareButton.isHidden = true
        
        // 剧集按钮
        listButton = ZMUtil.button(title: "剧集",  font: UIFont.systemFont(ofSize: 14), color: UIColor.white)
        listButton.isHidden = true
        
        ProtraitUI()
    }
    
    //MARK:横屏UI
    func landScapeUI() {
//        nextButton.isHidden = true
//        xuanzhuanBtnn.isHidden = true
//        likeButton.isHidden = true
//        listButton.isHidden = true
//        shareButton.isHidden = true
        
//        subviews.forEach({$0.removeFromSuperview()})
//        addSubview(bgMaskView)
//        addSubview(playBtn)
//        addSubview(playTimeLabel)
//        addSubview(ProgressView)
//        addSubview(allTimeLabel)
//        addLandScapeCons()
    }
    //MARK:横屏UI约束
    func addLandScapeCons() {
        bgMaskView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        playBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(30)
        }
                
        playTimeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(playBtn.snp.rightMargin).offset(15)
            make.centerY.equalToSuperview()
            make.width.equalTo(45)
        }
        
        ProgressView.snp.makeConstraints { (make) in
            make.left.equalTo(playTimeLabel.snp.rightMargin).offset(15)
            make.right.equalTo(allTimeLabel.snp.leftMargin).offset(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(24)
        }
        
        allTimeLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self.snp.right).offset(-15)
            make.centerY.equalToSuperview()
            make.width.equalTo(45)
        }
    }
    
    //MARK:竖屏UI
    func ProtraitUI() {
        addSubview(bgMaskView)
        addSubview(stack)
        stack.insertArrangedSubview(playBtn, at: 0)
        stack.insertArrangedSubview(nextButton, at: 1)
        stack.insertArrangedSubview(playTimeLabel, at: 2)
        stack.insertArrangedSubview(ProgressView, at: 3)
        stack.insertArrangedSubview(allTimeLabel, at: 4)
        stack.insertArrangedSubview(muteButton, at: 5)
        stack.insertArrangedSubview(xuanzhuanBtnn, at: 6)
        stack.insertArrangedSubview(downButton, at: 7)
        stack.insertArrangedSubview(likeButton, at: 8)
        stack.insertArrangedSubview(shareButton, at: 9)
        stack.insertArrangedSubview(listButton, at: 10)
        
//        subviews.forEach({$0.removeFromSuperview()})
//        addSubview(bgMaskView)
//        addSubview(playBtn)
//        addSubview(playTimeLabel)
//        addSubview(ProgressView)
//        addSubview(allTimeLabel)
//        addSubview(xuanzhuanBtnn)
        addProtraitCons()
    }
    
    //MARK:竖屏UI约束
    func addProtraitCons() {
        bgMaskView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        stack.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
        }
        
        playBtn.snp.makeConstraints { (make) in
            make.width.equalTo(30)
        }
        
        nextButton.snp.makeConstraints { (make) in
            make.width.equalTo(30)
        }
        
        playTimeLabel.snp.makeConstraints { (make) in
            make.width.equalTo(45)
        }
        
        ProgressView.snp.makeConstraints { (make) in
            make.height.equalTo(24)
        }
        
        allTimeLabel.snp.makeConstraints { (make) in
            make.width.equalTo(45)
        }
        
        muteButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
        }
        
        xuanzhuanBtnn.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
        }
        
        downButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
        }
        
        shareButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
        }
        
        likeButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
        }
        
        listButton.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
        }
    }
    
    func bindAction() {
        playBtn.rx.tap.subscribe { [weak self,weak playBtn] (_) in
            guard let self = self , let playBtn = playBtn else {return}
            guard let delegate = self.delegate else {return}
            delegate.playBtnClick(button: playBtn)
        }.disposed(by: disposeBag)
        
        nextButton.rx.tap.subscribe { [weak self] (_) in
           if let self = self,let delegate = self.delegate {
               delegate.playNextClick()
           }
        }.disposed(by: disposeBag)
        
        muteButton.rx.tap.subscribe { [weak self,weak muteButton] (_) in
            guard let self = self , let muteButton = muteButton else {return}
            guard let delegate = self.delegate else {return}
            delegate.muteClick(button: muteButton)
        }.disposed(by: disposeBag)
        
        xuanzhuanBtnn.rx.tap.subscribe {[weak self] (_) in
//            guard let self = self else {return}
//            AliyunUtil.setFullOrHalfScreen()
//            self.setNeedsLayout()
        }.disposed(by: disposeBag)
        
        downButton.rx.tap.subscribe { [weak self,weak downButton] (_) in
            if let self = self,let downButton = downButton,let delegate = self.delegate {
                delegate.downLoadClick(button: downButton)
            }
        }.disposed(by: disposeBag)
        
        likeButton.rx.tap.subscribe { [weak self,weak likeButton] (_) in
            if let self = self,let likeButton = likeButton,let delegate = self.delegate {
                delegate.lickButtonClick(button: likeButton)
            }
        }.disposed(by: disposeBag)
        
        shareButton.rx.tap.subscribe { [weak self] (_) in
            if let self = self,let delegate = self.delegate {
                delegate.shareButtonClick()
            }
        }.disposed(by: disposeBag)
        
        listButton.rx.tap.subscribe { [weak self] (_) in
            if let self = self,let delegate = self.delegate {
                delegate.listButtonClick()
            }
        }.disposed(by: disposeBag)
    }
    
}

extension PlayerViewBottomView : AliyunVodProgressViewDelegate {
    func aliyunVodProgressView(_ progressView: AliyunPlayerViewProgressView!, dragProgressSliderValue value: Float, event: UIControl.Event) {
        guard let delegate = delegate else {return}
        if event == .touchDown {
            // 开始拖动
            print("开始拖动")
            isDrag = true
            
        } else if event == .touchUpInside {
            // 拖动正常结束
            isDrag = false
            print("拖动正常结束")
            delegate.sliderDidEndDrag(value: value)
        } else if event == .valueChanged {
            delegate.sliderVauleDidChange(value: value)
        } else if event == .touchUpOutside {
            // 取消
                print("拖动取消")
            isDrag = false
        }
    }
    
    @objc func clickfuck(_ sdf : Float) {
        
    }
}
